# 简化的测试音频上传脚本
$uri = "http://localhost:8082/api/voiceprint/feature/add"
$filePath = "test_audio.wav"

Write-Host "开始测试WAV文件上传..."

if (-not (Test-Path $filePath)) {
    Write-Host "错误: 找不到文件 $filePath"
    exit 1
}

# 使用WebClient进行文件上传
$webClient = New-Object System.Net.WebClient
$webClient.Headers.Add("Content-Type", "multipart/form-data")

try {
    # 构建表单数据
    $boundary = "----WebKitFormBoundary" + [System.Guid]::NewGuid().ToString("N")
    $webClient.Headers["Content-Type"] = "multipart/form-data; boundary=$boundary"
    
    $fileBytes = [System.IO.File]::ReadAllBytes($filePath)
    $encoding = [System.Text.Encoding]::UTF8
    
    $formData = ""
    $formData += "--$boundary`r`n"
    $formData += "Content-Disposition: form-data; name=`"groupId`"`r`n`r`n"
    $formData += "group001`r`n"
    
    $formData += "--$boundary`r`n"
    $formData += "Content-Disposition: form-data; name=`"featureId`"`r`n`r`n"
    $formData += "test_wav_fix`r`n"
    
    $formData += "--$boundary`r`n"
    $formData += "Content-Disposition: form-data; name=`"featureInfo`"`r`n`r`n"
    $formData += "test wav fix`r`n"
    
    $formData += "--$boundary`r`n"
    $formData += "Content-Disposition: form-data; name=`"audioFile`"; filename=`"test_audio.wav`"`r`n"
    $formData += "Content-Type: audio/wav`r`n`r`n"
    
    $formDataBytes = $encoding.GetBytes($formData)
    $endBoundary = $encoding.GetBytes("`r`n--$boundary--`r`n")
    
    $totalBytes = New-Object byte[] ($formDataBytes.Length + $fileBytes.Length + $endBoundary.Length)
    [Array]::Copy($formDataBytes, 0, $totalBytes, 0, $formDataBytes.Length)
    [Array]::Copy($fileBytes, 0, $totalBytes, $formDataBytes.Length, $fileBytes.Length)
    [Array]::Copy($endBoundary, 0, $totalBytes, $formDataBytes.Length + $fileBytes.Length, $endBoundary.Length)
    
    $response = $webClient.UploadData($uri, "POST", $totalBytes)
    $responseText = $encoding.GetString($response)
    
    Write-Host "上传成功!"
    Write-Host "响应: $responseText"
}
catch {
    Write-Host "上传失败: $($_.Exception.Message)"
}
finally {
    if ($webClient) {
        $webClient.Dispose()
    }
}